<template>
  <div class="app-container">
    <div class="app-form">
      <ele-form
        v-model="formData"
        v-bind="formConfig"
        :request-fn="handleRequest"
        @request-success="handleRequestSuccess"
      />
    </div>
  </div>
</template>

<script>
import { commentCreate } from '@projectName/views/diandi_place/api/place/comment'
import { hotelList } from '@projectName/views/diandi_place/api/place/place'
export default {
  data() {
    return {
      formData: {
        status: 1
      },
      formConfig: {
        formDesc: {
          blocs: {
            type: 'cascader-bloc-store',
            label: '选择楼栋'
          },
          comment: {
            type: 'input',
            label: '评论内容'
          },
          hotel_id: {
            type: 'select',
            label: '楼栋ID',
            vif: (data) => data.blocs,
            // 这里必须制定 optionsLinkageFields 做为关联字段，当次字段值发生变化时，会重新出发请求
            optionsLinkageFields: ['blocs'],
            // Promise 对象
            options: async(data) => {
              const list = await hotelList({
                f_bloc_id: data.blocs[0],
                f_store_id: data.blocs[1]
              })
              const arr = [{ value: 0, text: '请选择楼栋' }]
              return arr.concat(list.data)
            }
          },
          labels: {
            type: 'input',
            label: '评价标签'
          },
          member_id: {
            type: 'input',
            label: '会员id'
          },
          order_id: {
            type: 'input',
            label: '订单id'
          },
          room_id: {
            type: 'input',
            label: '包间id'
          },
          star_num: {
            type: 'input',
            label: '综合星级'
          }
        },
        order: []
      }
    }
  },
  methods: {
    handleRequest(data) {
      data.bloc_id = data.blocs[0]
      data.store_id = data.blocs[1]
      commentCreate(data).then((response) => {
        if (response.code === 200) {
          this.$message.success(response.message)
          this.closePage()
        } else {
          this.$message.error(response.message)
        }
      })
      return Promise.resolve()
    },
    handleRequestSuccess() {},
    closePage() {
      this.$store.dispatch('app/closePage', {
        vm: this,
        fromName: this.$route.name,
        toName: 'diandi-place-place-comment-index',
        params: {}
      })
    }
  }
}
</script>
